package org.tangolyricstagger.gui;

import org.apache.commons.lang.StringUtils;
import org.eclipse.jface.action.ActionContributionItem;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class SettingsDialog extends Dialog {
	private SettingsManager settingsManager;
	private Text directory;
	private Text genres;
	private Text splitTitle;
	private Button fillComposerCheckButton;
	private Button appendRecordingYearCheckButton;
	private Button overwriteLyricsCheckButton;
	Image helpImage = GuiUtils.getImage("help.png");

	/**
	 * Create the dialog.
	 * 
	 * @param parentShell
	 */
	public SettingsDialog(Shell parentShell, SettingsManager settingsManager) {
		super(parentShell);
		helpImage = new Image(Display.getCurrent(), helpImage.getImageData().scaledTo(20, 20));

		this.settingsManager = settingsManager;
	}

	/**
	 * Create contents of the dialog.
	 * 
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		getShell().setText("Settings");
		Composite container = (Composite) super.createDialogArea(parent);
		GridLayout gridLayout = (GridLayout) container.getLayout();
		gridLayout.numColumns = 4;

		Label helpLabel;

		// directory
		Label directoryLabel = new Label(container, SWT.NONE);
		directoryLabel.setText("Music Library Folder");
		helpLabel = new Label(container, SWT.NONE);
		helpLabel.setImage(helpImage);
		helpLabel
		        .setToolTipText("The folder on your computer that contains your music library\n(specify a sub-folder if you don't want to run it on the whole thing.)");
		directory = new Text(container, SWT.BORDER);
		directory.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		DirectoryBrowseAction directoryBrowseAction = new DirectoryBrowseAction(getShell(), this);
		new ActionContributionItem(directoryBrowseAction).fill(container);

		// genres
		Label lblLimitScanTo = new Label(container, SWT.NONE);
		lblLimitScanTo.setText("Limit Scan To Genres Than Contain");
		helpLabel = new Label(container, SWT.NONE);
		helpLabel.setImage(helpImage);
		helpLabel
		        .setToolTipText("A comma-separated list of keywords (case-insensitive).\n\nEnables you to restrict which songs should be scanned for lyrics:\nIf the song genre contains any of these keywords, it will be scanned. Leave it empty to scan all genres.");
		genres = new Text(container, SWT.BORDER);
		genres.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		new Label(container, SWT.NONE);

		// split
		Label splitTitleLabel = new Label(container, SWT.NONE);
		splitTitleLabel.setText("Clean the title by ignoring text after");
		helpLabel = new Label(container, SWT.NONE);
		helpLabel.setImage(helpImage);
		helpLabel
		        .setToolTipText("ist of seperator characters. Helps to clean the title for matching with Todotango.\n\nAll remaing text in the song title after one of these separators (e.g. singer, year etc) is cleaned away.\nSo, if your song is called e.g. 'Poema - Roberto Maida', it will be cleand to 'Poema'.\nBut if your song is called 'Roberto Maida - Poema, I won't help you.");
		splitTitle = new Text(container, SWT.BORDER);
		splitTitle.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		new Label(container, SWT.NONE);

		// composer tag
		Label fillComposerLabel = new Label(container, SWT.NONE);
		fillComposerLabel.setText("Fill Composer Tag");
		helpLabel = new Label(container, SWT.NONE);
		helpLabel.setImage(helpImage);
		helpLabel.setToolTipText("Writes the composer/lyricist into the composer tag");
		fillComposerCheckButton = new Button(container, SWT.CHECK);
		new Label(container, SWT.NONE);

		// year
		Label appendRecordingYearLabel = new Label(container, SWT.NONE);
		appendRecordingYearLabel.setText("Append Recording Year To Lyrics");
		helpLabel = new Label(container, SWT.NONE);
		helpLabel.setImage(helpImage);
		helpLabel
		        .setToolTipText("Appends the recording year (taken from your existing year-tag) to the lyrics.\nUseful to easily see the recording year on your mobile player.");
		appendRecordingYearCheckButton = new Button(container, SWT.CHECK);
		new Label(container, SWT.NONE);

		// overwrite
		Label overwriteLyricsLabel = new Label(container, SWT.NONE);
		overwriteLyricsLabel.setText("Overwrite Lyrics If Already Present");
		helpLabel = new Label(container, SWT.NONE);
		helpLabel.setImage(helpImage);
		helpLabel.setToolTipText("Overwrites the lyrics if the song has already any. Disable it to speed up a repeated run.");
		overwriteLyricsCheckButton = new Button(container, SWT.CHECK);
		new Label(container, SWT.NONE);

		initValuesFromUserSettings();

		return container;
	}

	private void initValuesFromUserSettings() {
		Settings settings = settingsManager.getSettings();
		directory.setText(settings.getDirectory());
		genres.setText(settings.getGenres());
		splitTitle.setText(settings.getTakeTitleBeforeSeparators());
		fillComposerCheckButton.setSelection(settings.isFillComposerTag());
		appendRecordingYearCheckButton.setSelection(settings.isAppendRecordingYear());
		overwriteLyricsCheckButton.setSelection(settings.isOverwriteLyrics());
	}

	/**
	 * Create contents of the button bar.
	 * 
	 * @param parent
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		createButton(parent, IDialogConstants.OK_ID, "Save", true);
//		((GridLayout) parent.getLayout()).numColumns++;
//
//		Button defaults = new Button(parent, SWT.PUSH);
//		defaults.setText("Reset Defaults");
//		defaults.setFont(JFaceResources.getDialogFont());
//		defaults.addSelectionListener(new SelectionAdapter() {
//			public void widgetSelected(SelectionEvent event) {
//				settingsManager.r
//			}
//		});
		createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
	}

	/**
	 * Return the initial size of the dialog.
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(552, 300);
	}

	public Text getDirectory() {
		return directory;
	}

	@Override
	protected void okPressed() {
		String splitTitleChars = splitTitle.getText();
		splitTitleChars = StringUtils.deleteWhitespace(splitTitleChars);
		Settings settings = settingsManager.getSettings();
		settings.setDirectory(directory.getText());
		settings.setGenres(genres.getText());
		settings.setFillComposerTag(fillComposerCheckButton.getSelection());
		settings.setAppendRecordingYear(appendRecordingYearCheckButton.getSelection());
		settings.setOverwriteLyrics(overwriteLyricsCheckButton.getSelection());
		settings.setTakeTitleBeforeSeparators(splitTitleChars);
		settingsManager.writeSettings();
		super.okPressed();
	}
}
